package com.hd.service.sys.impl;

import com.hd.dao.BaseDao;
import com.hd.entity.sys.Log;
import com.hd.service.sys.LogService;
import com.hd.util.StringUtils;
import com.hd.util.result.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
*@author hzhh123
*@time 2018年1月30日上午10:57:17 
**/
@Service("logService")
public class LogServiceImpl implements LogService {
	 @Autowired
	 private BaseDao<Log,String>logDao;
	@Override
	public void save(Log log) {
		log.setId(UUID.randomUUID().toString());
		log.setCreateTime(new Date());
		logDao.save(log);

	}

	@Override
	public void delete(String id) {
		logDao.delete(Log.class, id);
	}

	@Override
	public Log get(String id) {
		return logDao.get(Log.class, id);
	}

	@Override
	public void selectDataGrid(PageInfo pageInfo,Log log) {
		 String hql="from Log where 1=1 ";
		 List<Object>params=new ArrayList<Object>();
		 if(StringUtils.isNotNull(log.getUsername())){
		 	hql+=" and username like ?";
		 	params.add("%"+log.getUsername()+"%");
		 }
		 if(StringUtils.isNotNull(log.getOptType())){
		 	hql+=" and optType =?";
		 	params.add(log.getOptType());
		 }
		 if(StringUtils.isNotNull(log.getCreateTimeStart())){
		 	hql+=" and createTime>=?";
		 	params.add(log.getCreateTimeStart());
		 }
		 if(StringUtils.isNotNull(log.getGetCreateTimeEnd())){
		 	hql+=" and createTime<=?";
		 	params.add(log.getGetCreateTimeEnd());
		 }
		 hql+=" order by createTime desc";
	     List<Log> logs=logDao.findPage(hql, pageInfo.getNowpage(), pageInfo.getPagesize(),params);
	     pageInfo.setRows(logs);
		String countHql="select count(*)  "+hql;
		int total=Integer.parseInt(logDao.getCountByList(countHql,params).toString());
	     pageInfo.setTotal(total);

	}

}
